package mypassForStu.tools;

import mypassForStu.entity.Apply;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class CreateTable {
    public static String path = "D:/mypass_src/";

    public static String createTable(Apply apply){
        BufferedImage image;
        //图片信息
        int row = 4, col = 6;
        int rowHeight = 60, colWidth = 300;
        int width = colWidth*6 + 20, height = row * rowHeight;

        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0,0, width, height);
        graphics.setColor(Color.darkGray);

        //画线
        for(int i = 0; i < row; i++){
            graphics.setColor(Color.black);
            graphics.drawLine(10, (i + 1)*rowHeight, width - 10, (i + 1)*rowHeight);
        }

        for(int i = 0; i < col + 1; i++){
            graphics.setColor(Color.black);
            graphics.drawLine(10 + i * colWidth, rowHeight, 10 + i * colWidth,  height - rowHeight);
        }

        String title = "请假单";
        graphics.drawString(title, width/3+10, rowHeight - 5);

        String[] head = {"学号", "学生姓名", "外出时间", "外出地点", "返校时间", "审批教师"};
        for(int i = 0; i < head.length; i++){
            graphics.drawString(head[i], 15 + colWidth*i, 2*rowHeight - 5);
        }

        graphics.drawString(String.valueOf(apply.getStudentId()), 15, 3*rowHeight-5);
        graphics.drawString(apply.getStudentName(), 15 + colWidth, 3*rowHeight-5);
        graphics.drawString(apply.getOutTime().toString(), 15 + colWidth*2, 3*rowHeight-5);
        graphics.drawString(apply.getPlace(), 15 + colWidth*3, 3*rowHeight-5);
        graphics.drawString(apply.getBackTime().toString(), 15 + colWidth*4, 3*rowHeight-5);
        graphics.drawString(apply.getTeacherName(), 15 + colWidth*5, 3*rowHeight-5);

        graphics.setColor(Color.red);
        String mark = "请将此图片保存作为假条";
        graphics.drawString(mark, 10, height - 10);

        String fileName = apply.getApplyId() + ".png";
        try {
            ImageIO.write(image, "PNG", new File(path + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return path + fileName;
    }
}
